import { defineConfig, loadEnv } from 'vite'
import type { UserConfig, ConfigEnv } from 'vite'
import react from '@vitejs/plugin-react'
import { resolve } from 'path'
// import OptimizationPersist from 'vite-plugin-optimize-persist'
// import PkgConfig from 'vite-plugin-package-config'

const root = process.cwd()
// https://vitejs.dev/config/
export default ({ command, mode }: ConfigEnv): UserConfig => {
  let env = {} as any
  const isBuild = command === 'build'
  if (!isBuild) {
    env = loadEnv((process.argv[3] === '--mode' ? process.argv[4] : process.argv[3]), root)
  } else {
    env = loadEnv(mode, root)
  }

  return {
    base: env.VITE_BASE_PATH,
    plugins: [react()],
    optimizeDeps:{
        include:[
          'jszip',
          'nprogress',
          'react-captcha-code',
          'xlsx',
          'react-redux',
          'react-router-dom',
          'antd',
          'axios'
        ]
    },
    server: {
      // host设置为true才可以使用network的形式，以ip访问项目
      host: true,

      // 端口号
      port: 8000,

      // 自动打开浏览器
      open: true,

      // 跨域设置允许
      cors: true,

      // 如果端口已占用直接退出
      strictPort: false,

      proxy: {
        // "/": {
        //   //设置代理地址
        //   target: "http://172.17.31.88:8090",
        //   //是否开启代理，v2默认为false v3默认为true
        //   changeOrigin: true,
        //   // 路径重写 把路径中的/api 替换成target
        // },
        "/api": {
          //设置代理地址
          target: "http://localhost:8090",
          //是否开启代理，v2默认为false v3默认为true
          changeOrigin: true,
          // 路径重写 把路径中的/api 替换成target
          rewrite: path => path.replace(/^\/api/, '')
        },
        "/echarts":{
          target:"https://echarts.apache.org",
          changeOrigin:true,
          rewrite: path => path.replace(/^\/echarts/, '')
        }
      },

    },
    resolve: {
      alias: {
        '@': resolve(__dirname, 'src'),
        '@assets': resolve(__dirname, 'src/assets'),
      }
    },
    build: {
      outDir: "C:/Users/hotqiu/Desktop/bs_bd2/src/main/resources/static"
    },
  }
}

